<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '货架'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      w
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-row>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="货架编号"
                hasFeedback
                prop="serialNumber"
              >
                <a-input
                  :disabled="title === '编辑'"
                  placeholder="请输入货架编号"
                  v-model.trim="model.serialNumber"
                  :maxLength="2"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="货架名称"
                hasFeedback
              >
                <a-input
                  placeholder="请输入货架名称"
                  v-model.trim="model.name"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="货架行数"
                hasFeedback
                prop="rowLine"
              >
                <a-input-number
                  style="width:100%"
                  :disabled="title === '编辑'"
                  placeholder="请输入货架行数"
                  v-model.trim="model.rowLine"
                  :min="0"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="货架列数"
                hasFeedback
                prop="col"
              >
                <a-input-number
                  style="width:100%"
                  :disabled="title === '编辑'"
                  placeholder="请输入货架列数"
                  v-model.trim="model.col"
                  :min="0"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="备注"
                hasFeedback
                prop="remark"
              >
                <a-input placeholder="请输入备注" v-model.trim="model.remark" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
const checkData = (rule, value, callback) => {
  if (value) {
    if (/[\u4E00-\u9FA5]/g.test(value) || /^[0-9]+.?[0-9]*$/g.test(value)) {
      callback(new Error("只可输入字母、不能输入汉字和数字!"));
    } else {
      callback();
    }
  }
  callback();
};
import api from "@/api/apiServer";
export default {
  props: {
    stockAreaId: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 900, // 弹框宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
    };
  },
  computed: {
    rules() {
      return {
        serialNumber: [
          { required: true, message: "请输入货架编号", trigger: "change" },
          // {
          //   validator: checkData,
          // },
        ],
        name: [
          { required: true, message: "请输入货架名称", trigger: "change" },
        ],
        rowLine: [
          { required: true, message: "请输入货架行数", trigger: "change" },
        ],
        col: [{ required: true, message: "请输入货架列数", trigger: "change" }],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.model = Object.assign({}, record);
      this.model.stockAreaId = this.stockAreaId;
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res = await api.ShelfAreaUpdate(this.model);
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        this.model = Object.assign({}, this.model);
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 900;
      }
    },
  },
};
</script>

<style></style>
